from views.base import BaseHandler
import json
from util.db import db
from util.comm import md5
from util.myjwt import myjwt
import time
# def kou():
#     def is_login(func):
#         def weip(self,*args,**kargs):
#             hearse = self.request.headers
#             try:
#                 token = hearse['token']
#                 flag = myjwt.jwt_check(token)
#                 if flag == True:
#                     payload = myjwt.jwt_de(token)
#                     ptime = payload['data']['exp']
#                     now = time.time()
#                     if int(now)-int(ptime)>36:
#                         self.finish({'code':400})
#                 else:
#                     self.finish({'code':401})
#             except:
#                 self.finish({'code':403})
#             func(self,*args,**kargs)
#         return weip
#     return is_login
def kou():
    def is_login(func):
        def wrapper(self,*args,**kargs):
            headers = self.request.headers
            try:
                token = headers['token']
                flag = myjwt.jwt_check(token)
                if flag ==True:
                    payload = myjwt.jwt_de(token)
                    ptime = payload['data']['exp']
                    now = time.time()
                    if int(now)-int(ptime)>36:
                        self.finish({'code':402})
                    else:
                        self.finish({'code':403})
            except:
                self.finish({'code':401})
            headers['token']
            func(self,*args,**kargs)
        return wrapper
    return is_login
class Deng(BaseHandler):
    def post(self):
        data = json.loads(self.request.body)
        name = data['name']
        password = data['password']
        if not all([name,password]):
            self.finish({'code':400,'msg':"用户名或密码不能为空"})
        try:
            sql = "select * from admin_user where username='%s'"%name
            res = db.fetchone(sql)
            if res['password'] == password:
                self.finish({'code':200,'msg':'登录成功',})
            else:
                self.finish({"code":400,'msg':'没有此用户'})
        except:
            self.finish({'code':400,'msg':'没有此角色'})
class Login(BaseHandler):
    def post(self):
        data = json.loads(self.request.body)
        name = data['name']
        password = data['password']
        if not all([name,password]):
            self.finish({'code':400,'msg':'用户名和密码不能为空'})
        sql = "select *  from admin_user where username='%s'"%name
        user = db.fetchone(sql)
        if user:
            newpwd = md5(password)
            if newpwd == user['password']:
                data = {'data':{'username':user['username'],'userid':user['id'],'exp':time.time()}}
                token = myjwt.jwt_en(data)

                self.finish({'code':200,'msg':'登录成功','name':user['name'],'roleid':user['roleid'],'id':user['id'],'token':token,'mpstatus':user['mpstatus']})
            else:
                self.finish({'code':400,'msg':'密码错误'})
        else:
            self.finish({'code':400,'msg':"密码不存在"})

class Xou(BaseHandler):
    @kou()
    def post(self):
        data = json.loads(self.request.body)
        id = data['id']
        print(id)
        password = data['password']
        pwd = md5(password)
        sql = "update admin_user set password='%s',mpstatus=%d where id=%d"%(pwd,2,int(id))
        db.add(sql)
        db.commit()
        self.finish({'code':200,'msg':'修改成功'})


def gettype(roleid):
    sql = "select * from role_resource as rr inner join resource as re on rr.resourceid=re.id where userid=%d and re.type=2"%(int(roleid))
    res = db.fetchall(sql)
    urls = []
    for i in res:
        url = i['url']
        urls.append(url)
    return urls






